#include<cstdio>
#include<iostream>
using namespace std;
const int max_monkeys = 200003;
const int max_time=400003;
const int INF=1e9;

int n,m;
int son[3][max_monkeys];
struct fy{int id,son;} give_up[max_time];
bool mark[3][max_monkeys];
int root[max_monkeys];
int ans[max_monkeys];

inline int read()
{
	int f=1,x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}


int getroot(int x)
{
    if(root[x]==x)
		return x;
    int temp=getroot(root[x]);
    ans[x]=min(ans[x],ans[root[x]]);
    return root[x]=temp;
}

int Union(int x,int y,int w)
{
    x=getroot(x),y=getroot(y);
    if(x!=y) 
    {
        if(x==1)
		{
			root[y]=x;
			ans[y]=w;
		}
        else
		{
			root[x]=y;
			ans[x]=w;
		}
    }
}

int main()
{
	freopen("monkey.in","r",stdin);
	freopen("monkey.out","w",stdout);
    n=read();
    m=read();
    for(int i=1;i<=n;++i) 
    {
        son[1][i]=read(),son[2][i]=read();
        root[i]=i,ans[i]=INF;
    }
    for(int i=1;i<=m;++i)
    {
        give_up[i].id=read();
		give_up[i].son=read();
        mark[give_up[i].son][give_up[i].id]=true;
    }
    for(int i=1;i<=n;++i)
    {
        if(!mark[1][i]&&son[1][i]!=-1)
			Union(i,son[1][i],m);
        if(!mark[2][i]&&son[2][i]!=-1)
			Union(i,son[2][i],m);
    }
	for(int i=1;i<=n;++i)
		if(getroot(i)==1)
			ans[i]=INF;
    for(int i=m;i>=1;--i)
    {
        if(son[give_up[i].son][give_up[i].id]!=-1)
			Union(give_up[i].id,son[give_up[i].son][give_up[i].id],i-1);
    }
    for(int i=1;i<=n;++i)
	{
		getroot(i);
		if(ans[i] == INF)
			cout<<-1<<endl;
		else
			cout<<ans[i]<<endl;
	}
}
